<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ConexionBD
 *
 * @author Jose
 */
class ConexionBD {
    static function conectar() {
        $link=mysql_connect("localhost","root","");
        if (!$link) {
            die('No pudo  conectarse: ' . mysql_error());
        }
        mysql_select_db("crea",$link);
        return $link;
    }
    static function ejecutarQuerySimple($sql) {
        $conn=self::conectar();
        $result = mysql_query($sql);
        $return=false;
        if (!$result) {
            error_log("Could not successfully run query ($sql) from DB: " . mysql_error());
            $return= "Error";
        }
        mysql_close($conn);

    }

    static function ejecutarQuery($sql,$campos) {
        $conn=self::conectar();
        $campos_cadena="";

        foreach($campos as $key=>$campo){
            if ($key!=0){
               $campos_cadena.=",";
            }
            if ($campo['tipo']=="string")
                $campos_cadena.="'".mysql_real_escape_string($campo['valor'],$conn)."'";
            else
                if ($campo['tipo']=="fechaNow")
                    $campos_cadena.="now()";
                else
                $campos_cadena.=$campo['valor'];
        }
        $sql=$sql." (".$campos_cadena.")";

        $result = mysql_query($sql);
        $id=mysql_insert_id();
        $return=false;
        if (!$result) {
            error_log("Could not successfully run query ($sql) from DB: " . mysql_error());
            $return= "Error";
        }
        mysql_close($conn);
        return !$return?$id:$return;
    }

    function ejecutarQueryUpdate($sql,$campos,$where){
        $conn=self::conectar();
        $campos_cadena="";

        foreach($campos as $key=>$campo){
            if ($key!=0){
               $campos_cadena.=",";
            }
            if ($campo['tipo']=="string")
                $campos_cadena.=$campo['nombre']."='".mysql_real_escape_string($campo['valor'],$conn)."'";
            else
                $campos_cadena.=$campo['nombre']."=".$campo['valor'];
        }
        $sql=$sql." ".$campos_cadena." ".$where;

        $result = mysql_query($sql);
        $return=false;
        if (!$result) {
            error_log("Could not successfully run query ($sql) from DB: " . mysql_error());
            $return= "Error";
        }
        mysql_close($conn);
        return !$return?$id:$return;
    }

    static function ejecutarQuerySelectUnitario($sql) {
        $conn=self::conectar();
        $result = mysql_query($sql);

        if (!$result) {
            error_log("Could not successfully run query ($sql) from DB: " . mysql_error());
            $return= "Error";
        }
        else
        {
            if (mysql_num_rows($result) == 0) {
                error_log("No rows found, nothing to print so am exiting $sql");
                $return= false;
            }
            while ($row = mysql_fetch_assoc($result)) {
                $return= $row;
            }
            mysql_free_result($result);
        }
        mysql_close($conn);

        return $return;
    }
    static function ejecutarQuerySelect($sql) {
        $conn=self::conectar();
        $result = mysql_query($sql);
        $return=  array();
        if (!$result) {
            error_log("Could not successfully run query ($sql) from DB: " . mysql_error());
            $return= "Error";
        }
        else
            {
                if (mysql_num_rows($result) == 0) {
                    error_log("No rows found, nothing to print so am exiting $sql");
                }
                if (is_array($return))
                    while ($row = mysql_fetch_assoc($result)) {
                        $return[]= $row;
                    }
            }

        mysql_free_result($result);
        mysql_close($conn);
        return $return;
    }
        static function ejecutarQueryInsert($sql) {
        $conn=self::conectar();
        $result = mysql_query($sql);
        $return=false;
        if (!$result) {
            error_log("Could not successfully run query ($sql) from DB: " . mysql_error());
            $return= "Error";
        }
        mysql_close($conn);

        }
}
?>
